//% color=#006400 iconWidth=50 iconHeight=40
namespace Tinybit {


	
	
    //% block="Car Run[index]" blockType="command"
    //% index.shadow="dropdown" index.options="CarState"
    export function Run(parameter: any, block: any){
        let index = parameter.index.code;
        Generator.addImport("from Tinybit import *");		
        Generator.addInitHeader(`Tinybit`,`tinybit = Tinybit()`); 	 
        Generator.addCode(`tinybit.Run(${index})`);
    }
	

    //% block="Car Run[index] Speed[speed]" blockType="command"
    //% index.shadow="dropdown" index.options="CarState"
	//% speed.shadow="range" speed.params.min=0 speed.params.max=255 speed.defl=0
    export function Run_Speed(parameter: any, block: any){
        let index = parameter.index.code;
		let speed = parameter.speed.code;
        Generator.addImport("from Tinybit import *");	
        Generator.addInitHeader(`Tinybit`,`tinybit = Tinybit()`); 	 
        Generator.addCode(`tinybit.Run_Speed(${index}, ${speed})`);
    }
	
	
	
	//% block="Car Run[index] Speed1[speed1] Speed2[speed2]" blockType="command"
    //% index.shadow="dropdown" index.options="CarState"
	//% speed1.shadow="range" speed1.params.min=0 speed1.params.max=255 speed1.defl=0
	//% speed2.shadow="range" speed2.params.min=0 speed2.params.max=255 speed2.defl=0
    export function Run_Speed2(parameter: any, block: any){
        let index = parameter.index.code;
		let speed1 = parameter.speed1.code;
		let speed2 = parameter.speed2.code;
        Generator.addImport("from Tinybit import *");	
        Generator.addInitHeader(`Tinybit`,`tinybit = Tinybit()`); 	 
        Generator.addCode(`tinybit.Run_Speed2(${index}, ${speed1}, ${speed2})`);
    }
	
	
	

    //% block="Car Stop" blockType="command"
    export function Stop(parameter: any, block: any){
        Generator.addImport("from Tinybit import *");	
        Generator.addInitHeader(`Tinybit`,`tinybit = Tinybit()`); 	
        Generator.addCode(`tinybit.Stop()`);
    }
	
	
	//% externalFunc
    export function getColorsFunc_() {
        return [
            "#fff", "#f00", "#0f0", "#00f", "#ff0", "#0ff", "#f0f", "#000"
        ]
    }
	//% block="Car HeadLight Color RGB value[COLOR]" blockType="command"
	//% COLOR.shadow="colorPalette" COLOR.params.column=4
    //% COLOR.params.colorsFunc="getColorsFunc_"
    export function HeadLightColor(parameter: any, block: any){
		let color=parameter.COLOR.code; //获取传进来的值
        var r = 0;
        var g = 0;
        var b = 0;
        try {
            if ( color.length == 8 ) {//分别截取RGB值然后转换为数字值
                r = parseInt(color.substring(2, 4), 16);
                g = parseInt(color.substring(4, 6), 16);
                b = parseInt(color.substring(6, 8), 16);
            }
        } catch(e) {
            return '';
        }
        Generator.addImport("from Tinybit import *");	
        Generator.addInitHeader(`Tinybit`,`tinybit = Tinybit()`); 	 
        Generator.addCode(`tinybit.HeadRGB(${r},${g},${b})`);
    }
	
	

	//% block="Car HeadLight Color Red Value[RValue] Green Value[GValue] Blue Value[BValue]" blockType="command"
	//% RValue.shadow="range" RValue.params.min=0 RValue.params.max=255 RValue.defl=0
	//% GValue.shadow="range" GValue.params.min=0 GValue.params.max=255 GValue.defl=0
	//% BValue.shadow="range" BValue.params.min=0 BValue.params.max=255 BValue.defl=0
    export function HeadLightRGB(parameter: any, block: any){
		let r = parameter.RValue.code;
		let g = parameter.GValue.code;
		let b = parameter.BValue.code; 
        Generator.addImport("from Tinybit import *");
		Generator.addInitHeader(`Tinybit`,`tinybit = Tinybit()`); 	
        Generator.addCode(`tinybit.HeadRGB(${r},${g},${b})`);
    }
	
	//% block="Turn off the headlights" blockType="command"
    //% weight=20
    export function headlightClose(){
		Generator.addImport("from Tinybit import *");
		Generator.addInitHeader(`Tinybit`,`tinybit = Tinybit()`); 		
        Generator.addCode(`tinybit.headlight_close()`);
    }
	
	
    //% block="Ultrasonic distance unit [unit]" blockType="reporter"
    //% unit.shadow="dropdown" unit.options="SonarUnit" unit.defl="SonarUnit.Centimeters"
    export function ultrasonic(parameter: any, block: any): number {
        Generator.addImport("from mpython import *");
		Generator.addImport("from Tinybit import *");
		Generator.addInitHeader(`Tinybit`,`tinybit = Tinybit()`); 		
        Generator.addCode(`tinybit.ultrasonic(${unit})`);
    }
	
	
	//% block="[side]line sensor detected [state]" blockType="boolean"
    //% side.shadow="dropdown" side.options="LineSide" side.defl="LineState.Left"
    //% state.shadow="dropdown" state.options="LineState" state.defl="LineSide.FindLine"
    export function trackSide(parameter: any, block: any) {
        let side = parameter.side.code;
        let state = parameter.state.code;        
		Generator.addImport("from Tinybit import *");
        Generator.addInitHeader(`Tinybit`,`tinybit = Tinybit()`); 			
        Generator.addCode(`tinybit.is_track_side(${side},${state})`);
    }
}